#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
char g[N][N];
bool st[N][N];
const int dx[] = { 0,-1,0,1 }, dy[] = { -1,0,1,0 };
int n, m, k, T, cnt;
bool flag;
void dfs(int x, int y)
{
    st[x][y] = true;
    for (int i = 0; i < 4; i++)
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= m || g[a][b] == '0' || st[a][b]) continue;
        st[a][b] = true;
        dfs(a, b);
    }
}
int main()
{
    cin >> T;
    for (int t = 1; t <= T; t++)
    {
        flag = true;
        cin >> n >> m;
        for (int i = 0; i < n; i++) cin >> g[i];
        cin >> k;
        printf("Case #%d:\n", t);
        while (k--)
        {
            char op[2];
            cin >> op;
            if (*op == 'M')
            {
                int x, y, z;
                cin >> x >> y >> z;
                if (g[x][y] != z + '0')
                {
                    g[x][y] = z + '0';
                    flag = true;
                }
            }
            else
            {
                if (flag == false)
                {
                    cout << cnt << endl;
                    continue;
                }
                cnt = 0;
                memset(st, false, sizeof st);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (g[i][j] == '1' && !st[i][j])
                        {
                            dfs(i, j);
                            cnt++;
                        }
                    }
                }
                cout << cnt << endl;
            }
        }
    }
}